package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.SportsStopEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.IndicatorSeekBar;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

public class AchievementPoppupWindow extends PopupWindow {
    private ImageView iv_dismiss;
    private TextView tv_page_num;
    private MZBannerView mzbView;
    private Context context;
    private View layout;
    private List<SportsStopEntity.DataBean.SportMedalsBean> dataBeanList;

    public static AchievementPoppupWindow getInstance(Context context, List<SportsStopEntity.DataBean.SportMedalsBean> dataBeanList) {
        return new AchievementPoppupWindow(context, dataBeanList);
    }

    private AchievementPoppupWindow(Context context, List<SportsStopEntity.DataBean.SportMedalsBean> dataBeanList) {
        super(context);
        this.context = context;
        this.dataBeanList = dataBeanList;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }


    private void initView() {
        layout = View.inflate(context, R.layout.achievement_popup_window, null);
        mzbView = layout.findViewById(R.id.mzbView);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        tv_page_num = layout.findViewById(R.id.tv_page_num);
        mzbView.setIndicatorVisible(false);
        tv_page_num.setText(1 + "/" + dataBeanList.size());
        mzbView.setPages(dataBeanList, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder(dataBeanList);
            }
        });
        mzbView.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_page_num.setText((position + 1) + "/" + dataBeanList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public static class BannerViewHolder implements MZViewHolder<SportsStopEntity.DataBean.SportMedalsBean> {
        private LinearLayout mLlSportMedals, mLlPersonalLongMarchProgress;
        private ImageView mImageView;
        private TextView tv_medal, titleTextView, tvProgress, tvMileage, tvStartEndName, tvSurplusProgress;
        private IndicatorSeekBar indicator_seek_bar_long;
        private List<SportsStopEntity.DataBean.SportMedalsBean> listString = new ArrayList<SportsStopEntity.DataBean.SportMedalsBean>();

        public BannerViewHolder(List<SportsStopEntity.DataBean.SportMedalsBean> lists) {
            listString.clear();
            for (int i = 0; i < lists.size(); i++) {
                listString.add(lists.get(i));
            }
        }

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.item_mbz_photo_view, null);
            mImageView = view.findViewById(R.id.iv_banner_image);
            tv_medal = view.findViewById(R.id.tv_medal);
            titleTextView = view.findViewById(R.id.titleTextView);
            mLlSportMedals = view.findViewById(R.id.ll_sport_medals);
            mLlPersonalLongMarchProgress = view.findViewById(R.id.ll_personal_long_march_progress);
            tvMileage = view.findViewById(R.id.tv_mileage);
            tvProgress = view.findViewById(R.id.tv_progress);
            tvStartEndName = view.findViewById(R.id.tv_start_end_name);
            tvSurplusProgress = view.findViewById(R.id.tv_surplus_progress);
            indicator_seek_bar_long = view.findViewById(R.id.indicator_seek_bar_long);
            return view;
        }

        long pId;

        @Override
        public void onBind(final Context context, final int position, final SportsStopEntity.DataBean.SportMedalsBean data) {
            if (data.personalLongMarchProgress == null) {
                // 数据绑定
                GlideUtils.loadImage(context, data.url, mImageView);
                tv_medal.setTextColor(Color.parseColor(data.color));
                titleTextView.setTextColor(Color.parseColor(data.color));
                tv_medal.setText(data.name);
                mLlSportMedals.setVisibility(View.VISIBLE);
                mLlPersonalLongMarchProgress.setVisibility(View.GONE);
            } else {
                mLlSportMedals.setVisibility(View.GONE);
                mLlPersonalLongMarchProgress.setVisibility(View.VISIBLE);
                tvStartEndName.setText(data.personalLongMarchProgress.startPoint + "-" + data.personalLongMarchProgress.endPoint);

                String string = data.personalLongMarchProgress.mileage + "km";
                SpannableString spanColor = new SpannableString(string);
                spanColor.setSpan(new AbsoluteSizeSpan(14,true), string.length()-2, string.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tvMileage.setText(spanColor);

                String string1 = CommonUtil.Double2position(data.personalLongMarchProgress.addProgress * 100) + "%";
                SpannableString spanColor1 = new SpannableString(string1);
                spanColor1.setSpan(new AbsoluteSizeSpan(14,true), string1.length() - 1, string1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tvProgress.setText(spanColor1);

                String surplus = "距离目的地剩余:%s";
                tvSurplusProgress.setText(String.format(surplus, CommonUtil.Double2position(data.personalLongMarchProgress.surplusProgress * 100) + "%"));

                indicator_seek_bar_long.setProgress((int) (data.personalLongMarchProgress.progress * 100));
                indicator_seek_bar_long.setDoubleProgress(data.personalLongMarchProgress.progress * 100);
            }
        }
    }
}
